DEFINE TEMP-TABLE ttAktie NO-UNDO
   FIELD aktienr          LIKE aktie.aktienr
   FIELD Descrizione      AS CHARACTER FORMAT "X(50)"
   FIELD Closed           AS LOGICAL 
INDEX indAktieNr IS UNIQUE aktienr.

DEFINE TEMP-TABLE ttBest NO-UNDO
   FIELD aktienr LIKE aktie.aktienr
   FIELD rBEST   AS ROWID
INDEX indAktieNr aktienr.

DEFINE VARIABLE ipdtDaData AS DATE       NO-UNDO.
DEFINE VARIABLE ipdtAData  AS DATE       NO-UNDO.
DEFINE VARIABLE lvdtData   AS DATE       NO-UNDO.
DEFINE VARIABLE ipiProject AS INTEGER    NO-UNDO.
DEFINE VARIABLE ipcMedList AS CHARACTER  NO-UNDO.


ASSIGN
   ipdtDaData = DATE(6, 16, 2006)
   ipdtAData  = DATE(11,30,2006)
   ipiProject = 20 /* Comune Milano Manutenzione 2006 */
   ipcMedList = "*"
.

DO lvdtData = ipdtDaData TO ipdtAData:
   FOR EACH besteding NO-LOCK
   WHERE besteding.datum     = lvdtData
   AND   besteding.projektnr = ipiProject
   AND   CAN-DO(ipcMedList, besteding.medkode) = TRUE
   AND  (besteding.bevestigd = FALSE OR besteding.klrefnr = 0):

      FIND FIRST ttAktie
      WHERE ttAktie.AktieNr = besteding.aktienr NO-ERROR.
      IF NOT AVAILABLE ttAktie THEN DO:

         CREATE ttAktie.
         ASSIGN
            ttAktie.AktieNr = besteding.aktienr
            ttAktie.Closed  = TRUE
         .
         
         FIND  aktie NO-LOCK
         WHERE aktie.aktienr = ttAktie.aktienr NO-ERROR.
         IF AVAILABLE aktie THEN DO:
            /* Trovato la richiesta */
            ttAktie.Descrizione = aktie.omschrijving[1].
         END.
      END.
      CREATE ttBest.
      ASSIGN
         ttBest.aktienr = ttAktie.aktienr
         ttBest.rBEST   = ROWID(besteding)
      .
   END.
END.

FOR EACH ttAktie:
   DISPLAY
      ttAktie
   .
   UPDATE
      ttaktie.closed
   .
END.

FOR EACH ttAktie
WHERE ttAktie.closed = TRUE,
EACH ttBest 
WHERE ttBest.aktienr = ttAktie.aktienr,
FIRST besteding
WHERE ROWID(besteding) = ttBest.rBEST:
   ASSIGN
      besteding.bevestigd = TRUE
   .
END.

